/*
 *  * Copyright (C) 2021 Huawei Device Co., Ltd.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.example.smileyrating.slice;

import com.example.smileyrating.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Button btn_smileyrating2;

    private Button btn_smileyrating;

    private Button smiley_rating2_list, btn_smiley_view;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_ability_main, null, false);

        btn_smileyrating = (Button) rootLayout.findComponentById(ResourceTable.Id_smiley_rating);
        btn_smileyrating.setClickedListener(this);

        btn_smileyrating2 = (Button) rootLayout.findComponentById(ResourceTable.Id_smiley_rating2);
        btn_smileyrating2.setClickedListener(this);

        smiley_rating2_list = (Button) rootLayout.findComponentById(ResourceTable.Id_smiley_rating2_list);
        smiley_rating2_list.setClickedListener(this);

        btn_smiley_view = (Button) rootLayout.findComponentById(ResourceTable.Id_smiley_view);
        btn_smiley_view.setClickedListener(this);

        super.setUIContent(rootLayout);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {

        Operation operation1 = new Intent.OperationBuilder().withBundleName("com.example.smileyrating")
            .withAbilityName("com.example.smileyrating.SmileRatingAbility")
            .build();
        Intent intent_smile = new Intent();

        intent_smile.setOperation(operation1);

        Operation operation = new Intent.OperationBuilder().withBundleName("com.example.smileyrating")
            .withAbilityName("com.example.smileyrating.SmileyRating2")
            .build();
        Intent intent = new Intent();

        intent.setOperation(operation);

        intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        switch (component.getId()) {
            case ResourceTable.Id_smiley_rating:
                startAbility(intent_smile);
                break;
            case ResourceTable.Id_smiley_rating2:
                startAbility(intent);
                break;
            case ResourceTable.Id_smiley_rating2_list:
                Intent intent1 = new Intent();
                intent1.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
                present(new ListItemProviderExample(), intent1);
                break;

            case ResourceTable.Id_smiley_view:
                Intent intent2 = new Intent();
                intent2.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
                present(new TestSlice(), intent2);
                break;
            default:
                break;
        }
    }
}
